<template>
  <div>
    <el-card>
      <el-row>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">设备管理</el-breadcrumb-item>
          <el-breadcrumb-item> 设备档案</el-breadcrumb-item>
        </el-breadcrumb>
      </el-row>
      <!--1.多条件查询栏 -->
      <el-row>
        <el-col>
          <el-form
            :inline="true"
            :model="deviceSearch"
            class="demo-form-inline"
            style="margin-top: 10px"
          >
            <el-form-item label="小区">
              <el-select
                clearable
                v-model="deviceSearch.cid"
                placeholder="请选择小区"
              >
                <el-option
                  v-for="item of cellList"
                  :key="item.cid"
                  :label="item.cname"
                  :value="item.cid"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="设备名称">
              <el-input
                v-model="deviceSearch.dname"
                placeholder="请输入设备名称"
              ></el-input>
            </el-form-item>

            <el-form-item label="设备编码">
              <el-input
                v-model="deviceSearch.dnumber"
                placeholder="请输入设备编码"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button
                icon="el-icon-search"
                type="primary"
                circle
                @click="getByCondition()"
              ></el-button>
              <el-button type="primary" @click="clearSearchForm()"
                >清空</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-divider></el-divider>

      <!--2.新增、批量删除按钮 -->
      <el-row style="margin-top: 40px; margin-bottom: 10px">
        <el-col :span="6" :offset="0">
          <el-button type="primary" @click="addClick()">添加</el-button>
          <el-button type="danger" @click="batchDeleteClick()"
            >批量删除</el-button
          >
        </el-col>
      </el-row>

      <!--3.设备表单 -->
      <el-row>
        <el-col>
          <el-table
            :data="deviceList"
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column label="序号" width="70">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.did }}</span>
              </template>
            </el-table-column>

            <el-table-column label="小区" width="100">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{
                  scope.row.cell.cname
                }}</span>
              </template>
            </el-table-column>
            <el-table-column label="设备名称" width="100" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.dname }}</span>
              </template>
            </el-table-column>
            <el-table-column label="设备编号" width="100" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.dnumber }}</span>
              </template>
            </el-table-column>
            <el-table-column label="类型" width="100" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.dtype }}</span>
              </template>
            </el-table-column>
            <el-table-column label="状态" width="100" align="center">
              <template slot-scope="scope">
                <div
                  style="margin-left: 10px"
                  :class="statusClass(scope.row.status)"
                >
                  {{ getStatus(scope.row.status) }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="生产厂家" width="150" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{
                  scope.row.manufacturer
                }}</span>
              </template>
            </el-table-column>
            <el-table-column label="责任人" width="80" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{
                  scope.row.principal.uname
                }}</span>
              </template>
            </el-table-column>
            <el-table-column label="联系电话" width="120" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{
                  scope.row.principal.phone
                }}</span>
              </template>
            </el-table-column>
            <el-table-column label="安装时间" width="120" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{
                  scope.row.createtime
                }}</span>
              </template>
            </el-table-column>
            <el-table-column label="保养次数" width="50" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{
                  scope.row.maintaintimes
                }}</span>
              </template>
            </el-table-column>
            <el-table-column label="维修次数" width="50" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{
                  scope.row.repairtimes
                }}</span>
              </template>
            </el-table-column>

            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  round
                  plain
                  size="mini"
                  type="primary"
                  @click="handleEdit(scope.$index, scope.row)"
                  >修改</el-button
                >
                <el-button
                  plain
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>

      <!--4.分页 -->
      <el-row style="margin-top: 40px">
        <el-col>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[2, 3, 4, 5]"
            :page-size="size"
            layout="prev, pager, next, jumper,total, sizes"
            :total="total"
          >
          </el-pagination>
        </el-col>
      </el-row>

      <!-- 5.添加修改对话框-->
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogFormVisible"
        @close="closeDialog"
        center
      >
        <el-row>
          <el-col :span="16" :offset="4">
            <el-form :model="deviceModify" label-width="100px">
              <el-form-item label="小区">
                <el-select
                  v-model="deviceModify.cell.cid"
                  placeholder="请选择小区"
                  style="width: 450px"
                >
                  <el-option
                    v-for="item of cellList"
                    :key="item.cid"
                    :label="item.cname"
                    :value="item.cid"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="设备名称" prop="dname">
                <el-input v-model="deviceModify.dname"></el-input>
              </el-form-item>

              <el-form-item label="设备编码" prop="dnumber">
                <el-input v-model="deviceModify.dnumber"></el-input>
              </el-form-item>
              <el-form-item label="设备类型" prop="dtype">
                <el-input v-model="deviceModify.dtype"></el-input>
              </el-form-item>

              <el-form-item label="设备单位" prop="dunit">
                <el-input v-model="deviceModify.dunit"></el-input>
              </el-form-item>
              <el-form-item label="生产厂家" prop="manufacturer">
                <el-input v-model="deviceModify.manufacturer"></el-input>
              </el-form-item>

              <el-form-item label="责任人">
                <el-select
                  v-model="deviceModify.principal.uid"
                  placeholder="请选择责任人"
                  style="width: 450px"
                >
                  <el-option
                    v-for="item of userList"
                    :key="item.uid"
                    :label="item.uname"
                    :value="item.uid"
                  ></el-option>
                </el-select>
              </el-form-item>

              <el-form-item>
                <el-row style="margin-top: 50px">
                  <el-col :span="10" :offset="7">
                    <el-button
                      type="primary"
                      @click="addDevice()"
                      v-if="dialogTitle == '添加设备'"
                      >添加</el-button
                    >
                    <el-button
                      type="primary"
                      @click="modifyDevice()"
                      v-if="dialogTitle == '修改设备'"
                      >修改</el-button
                    >

                    <el-button
                      type="info"
                      @click="closeDialog"
                      style="margin-left: 40px"
                      >取消</el-button
                    >
                  </el-col>
                </el-row>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { Result } from "element-ui";
export default {
  data() {
    return {
      //多条件查询
      deviceSearch: {},
      cellList: [],
      userList: [],

      //表格
      deviceList: [],

      //分页
      page: 1,
      size: 3,
      total: 0,
      isPageChangeToOne: true,

      //添加修改表单
      dialogTitle: "添加设备",
      dialogFormVisible: false,
      deviceModify: {
        cell: { cname: "" },
        principal: { uname: "" },
      },
    };
  },
  methods: {
    //多条件查询
    getByCondition() {
      this.$axios
        .post(
          "/device/getByCondition/" + this.page + "/" + this.size,
          this.deviceSearch
        )
        .then((respData) => {
          console.log(respData);
          this.deviceList = respData.data.data.beanList;
          this.page = respData.data.data.pageIndex;
          this.total = respData.data.data.totalRecord;
        });
    },
    //清空多条件查询表单
    clearSearchForm() {
      this.deviceSearch = {};
      this.getByCondition();
    },
    getCellList() {
      this.$axios.get("/cell/getAll").then((respData) => {
        this.cellList = respData.data.data;
      });
    },
    getUserList() {
      this.$axios.get("/user/getAll").then((respData) => {
        this.userList = respData.data.data;
      });
    },
    // -----------------------表格
    //添加按钮
    addClick() {
      this.dialogFormVisible = true;
    },
    //批量删除按钮
    batchDeleteClick() {
      if (this.multipleSelection.length == 0) {
        this.$message.error("请至少选择一条数据！");
      } else {
        this.$confirm("确定删除这些数据吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let deleteParam = "?";
            for (var index in this.multipleSelection) {
              deleteParam += "dids=" + this.multipleSelection[index].did + "&";
            }
            this.$axios
              .delete("/device/batchedDelete" + deleteParam)
              .then((respData) => {
                if (respData.data.data) {
                  this.$message.success("批量删除成功！");
                  this.getByCondition();
                }
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },
    //表格多选框事件
    handleSelectionChange(rows) {
      this.multipleSelection = rows;
      console.log(this.multipleSelection);
    },
    //表格编辑事件
    handleEdit(index, row) {
      this.dialogTitle = "修改设备";
      this.deviceModify = row;
      this.dialogFormVisible = true;
    },
    //表格删除事件
    handleDelete(index, row) {
      this.$confirm("确定删除" + row.dname + "这条信息吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .delete("/device/deleteById/" + row.did)
            .then((respData) => {
              console.log(respData);
              if (respData.data.data) {
                this.$message.success("删除成功！");
                this.getByCondition();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //数据渲染
    getStatus(status) {
      switch (status) {
        case 0:
          return "正常";
        case 1:
          return "待保养";
        case 2:
          return "待维修";
        case 3:
          return "已损坏";
        case 4:
          return "待排查";
      }
    },
    statusClass(status) {
      switch (status) {
        case 0:
          return "tag1";
        case 1:
          return "tag2";
        case 2:
          return "tag3";
        case 3:
          return "tag0";
        case 4:
          return "tag4";
      }
    },
    //--------------------------分页
    //分页尺寸变化
    handleSizeChange(size) {
      this.size = size;
      this.getByCondition();
    },
    //分页页码变化
    handleCurrentChange(page) {
      this.isPageChangeToOne = false;
      this.page = page;
      this.getByCondition();
      this.isPageChangeToOne = true;
    },

    //-----------------------------添加修改对话框
    //关闭添加修改对话框
    closeDialog() {
      this.deviceModify = {
        cell: { cname: "" },
        principal: { uname: "" },
      };
      this.dialogFormVisible = false;
    },
    //添加设备
    addDevice() {
      this.$axios
        .post("/device/addDevice", this.deviceModify)
        .then((respData) => {
          if (respData.data.data) {
            this.$message.success("添加成功！");
            this.deviceModify = {
              cell: { cname: "" },
              principal: { uname: "" },
            };
            this.dialogFormVisible = false;
            this.getByCondition();
          }
        });
    },
    //修改设备
    modifyDevice() {
      this.$axios
        .put("/device/modifyDevice", this.deviceModify)
        .then((respData) => {
          if (respData.data.data) {
            this.$message.success("修改成功！");
            this.deviceModify = {
              cell: { cname: "" },
              principal: { uname: "" },
            };
            this.dialogFormVisible = false;
            this.getByCondition();
          }
        });
    },
  },
  mounted() {
    this.getByCondition();
    this.getCellList();
    this.getUserList();
  },
};
</script>

<style>
.tag0 {
  text-align: center;
  border-radius: 12px;
  background: #e8f8f3;
  border: 1px solid #8f9593;
  color: #8f9593;
}

.tag1 {
  text-align: center;
  border-radius: 12px;
  background: #e8f8f3;
  border: 1px solid #21bd88;
  color: #21bd88;
}
.tag2 {
  text-align: center;
  border-radius: 12px;
  background: #e8f8f3;
  border: 1px solid #f3b509;
  color: #f3b509;
}
.tag3 {
  text-align: center;
  border-radius: 12px;
  background: #e8f8f3;
  border: 1px solid #fa556e;
  color: #fa5563;
}
.tag4 {
  text-align: center;
  border-radius: 12px;
  background: #e8f8f3;
  border: 1px solid #0a91e0;
  color: #0a91e0;
}
</style>